package cn.qinhao.pethome.employee.service.impl;

import cn.qinhao.pethome.basic.base.BaseConstants;
import cn.qinhao.pethome.basic.base.service.impl.BaseServiceImpl;
import cn.qinhao.pethome.employee.domain.Employee;
import cn.qinhao.pethome.employee.mapper.EmployeeMapper;
import cn.qinhao.pethome.employee.service.IEmployeeService;
import cn.qinhao.pethome.login.domain.Logininfo;
import cn.qinhao.pethome.login.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qinhao
 * @since 2023-02-23
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void add(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //设置和用户不同的字段
        logininfo.setDisable(BaseConstants.logininfo.STATE_disable_open);//设置状态1代表启用，0代表禁用
        logininfo.setType(BaseConstants.logininfo.STATE_type_admin);//设置为1代表用户，0为管理员
        //设置用户相同字段（使用工具类可以把两个相同的资源复制到指定的地方，前提是字段必须相同）
        BeanUtils.copyProperties(employee,logininfo);
        logininfoMapper.add(logininfo);
        employee.setLogininfoId(logininfo.getId());//使用的是mapper里面的主键回填

        super.add(employee);
    }

    @Override
    public Employee loadByLogininfo(Long id) {
        return employeeMapper.loadByLogininfo(id);
    }

    @Override
    public List<String> loadPermissionByEmpId(Long id) {
        return employeeMapper.loadPermissionByEmpId(id);
    }
}